/**
 * Copyright (C) 2009 Ignasi Fosch Alonso
 * This program is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation; either version 2 of the License, or (at your option) any later
 * version.
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more 
 * details.
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
 * Place, Suite 330, Boston, MA 02111-1307 USA
 *
 * You can reach me by mail at natx@y10k.ws
 * You can reach him by paper mail at:
 * C. LLEIDA, 41-43, 3r 1a
 * El Prat de Llobregat 08820
 * Barcelona, SPAIN
 */
package ws.y10k.goloim.interfaces.swt;

import java.util.Enumeration;

import org.eclipse.jface.window.Window;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.Text;

import ws.y10k.goloim.model.Pallet;

/**
 * @author natx
 *
 */
public class ManagePalletsWindow extends Window {
	
	Table tblPallets;
	private Label lblCode;
	Text txtCode;
	private Label lblDescription;
	Text txtDescription;
	private Label lblWidth;
	Text txtWidth;
	private Label lblHeight;
	Text txtHeight;
	private Label lblDepth;
	Text txtDepth;
	private Label lblWeight;
	Text txtWeight;
	TableItem itmSelected = null;
	private Button btnSave;
	private Button btnCancel;

	public ManagePalletsWindow(Window parent) {
		super(parent);
		this.setShellStyle(SWT.APPLICATION_MODAL | SWT.DIALOG_TRIM);
		this.open();
	}
	
	public Control createContents(Composite parent) {
		// TODO i18n

		parent.getShell().setSize(800, 400);
		parent.getShell().setText("Manage pallets");
		GoLoIm.center(parent.getShell());
		GridLayout lytGrid = new GridLayout();
		lytGrid.numColumns = 2;
		parent.getShell().setLayout(lytGrid);
		
		tblPallets = new Table(parent.getShell(), SWT.SINGLE | SWT.V_SCROLL);
		GridData grdData = new GridData();
		grdData.widthHint = parent.getShell().getBounds().width / 2;
		grdData.heightHint = parent.getShell().getBounds().height - 55;
		tblPallets.setLayoutData(grdData);
		tblPallets.setLinesVisible(true);
		tblPallets.setHeaderVisible(true);
		String[] titles = {"Code","Description","Width","Height","Depth","Weight"};
		for ( int i = 0; i < titles.length; i++ ) {
			TableColumn col = new TableColumn(tblPallets,SWT.NONE);
			col.setText(titles[i]);
		}
		Enumeration<Pallet> itBoxes = GoLoIm.pallets.enumeration();
		while ( itBoxes.hasMoreElements() ) {
			Pallet pallet = itBoxes.nextElement();
			TableItem itmBox = new TableItem(tblPallets,SWT.NONE);
			itmBox.setText(0,pallet.getCode());
			itmBox.setText(1,pallet.getDescription());
			itmBox.setText(2,((Double) pallet.getWidth()).toString());
			itmBox.setText(3,((Double) pallet.getHeight()).toString());
			itmBox.setText(4,((Double) pallet.getDepth()).toString());
			itmBox.setText(5,((Double) pallet.getWeight()).toString());
		}
		for ( int i = 0; i < titles.length; i++ ) {
			tblPallets.getColumn(i).pack();
		}
		tblPallets.addListener (SWT.Selection, new Listener () {
			@Override
			public void handleEvent (Event e) {
				TableItem [] selection = tblPallets.getSelection ();
				if ( itmSelected == null || !itmSelected.equals(selection[0]) ) {
					txtCode.setText(selection[0].getText(0));
					txtDescription.setText(selection[0].getText(1));
					txtWidth.setText(selection[0].getText(2));
					txtHeight.setText(selection[0].getText(3));
					txtDepth.setText(selection[0].getText(4));
					txtWeight.setText(selection[0].getText(5));
					itmSelected = selection[0];
				} else {
					itmSelected = null;
					tblPallets.deselectAll();
					txtCode.setText("");
					txtDescription.setText("");
					txtWidth.setText("");
					txtHeight.setText("");
					txtDepth.setText("");
					txtWeight.setText("");
				}
			}
		});

		
		Composite cmpForm = new Composite(parent.getShell(), SWT.BORDER);
		grdData = new GridData();
		grdData.widthHint = (parent.getShell().getBounds().width / 2) - 45;
		grdData.heightHint = parent.getShell().getBounds().height - 50;
		cmpForm.setLayoutData(grdData);
		cmpForm.setSize(tblPallets.getBounds().height,(parent.getShell().getBounds().width / 2) - 45);
		cmpForm.setLocation(tblPallets.getBounds().width,0);
		GridLayout lytGridForm = new GridLayout();
		lytGridForm.numColumns = 2;
		cmpForm.setLayout(lytGridForm);

		lblCode = new Label(cmpForm, SWT.NONE);
		lblCode.setText("&Code");
		grdData = new GridData();
		grdData.widthHint = 85;
		lblCode.setLayoutData(grdData);
		txtCode = new Text(cmpForm, SWT.BORDER);
		grdData = new GridData();
		grdData.widthHint = 150;
		txtCode.setLayoutData(grdData);
		lblDescription = new Label(cmpForm, SWT.NONE);
		lblDescription.setText("De&scription");
		grdData = new GridData();
		grdData.widthHint = 85;
		lblDescription.setLayoutData(grdData);
		txtDescription = new Text(cmpForm, SWT.BORDER);
		grdData = new GridData();
		grdData.widthHint = 150;
		txtDescription.setLayoutData(grdData);
		lblWidth = new Label(cmpForm, SWT.NONE);
		lblWidth.setText("&Width");
		grdData = new GridData();
		grdData.widthHint = 85;
		lblWidth.setLayoutData(grdData);
		txtWidth = new Text(cmpForm, SWT.BORDER);
		grdData = new GridData();
		grdData.widthHint = 150;
		txtWidth.setLayoutData(grdData);
		lblHeight = new Label(cmpForm, SWT.NONE);
		lblHeight.setText("&Height");
		grdData = new GridData();
		grdData.widthHint = 85;
		lblHeight.setLayoutData(grdData);
		txtHeight = new Text(cmpForm, SWT.BORDER);
		grdData = new GridData();
		grdData.widthHint = 150;
		txtHeight.setLayoutData(grdData);
		lblDepth = new Label(cmpForm, SWT.NONE);
		lblDepth.setText("&Depth");
		grdData = new GridData();
		grdData.widthHint = 85;
		lblDepth.setLayoutData(grdData);
		txtDepth = new Text(cmpForm, SWT.BORDER);
		grdData = new GridData();
		grdData.widthHint = 150;
		txtDepth.setLayoutData(grdData);
		lblWeight = new Label(cmpForm, SWT.NONE);
		lblWeight.setText("W&eight");
		grdData = new GridData();
		grdData.widthHint = 85;
		lblWeight.setLayoutData(grdData);
		txtWeight = new Text(cmpForm, SWT.BORDER);
		grdData = new GridData();
		grdData.widthHint = 150;
		txtWeight.setLayoutData(grdData);
		btnSave = new Button(cmpForm, SWT.PUSH);
		grdData = new GridData();
		grdData.widthHint = 65;
		grdData.horizontalAlignment = SWT.CENTER;
		btnSave.setLayoutData(grdData);
		btnSave.setText("&Save");
		btnSave.addSelectionListener(new SavePalletSelectionListener(this));
		btnCancel = new Button(cmpForm, SWT.PUSH);
		grdData = new GridData();
		grdData.widthHint = 65;
		grdData.horizontalAlignment = SWT.CENTER;
		btnCancel.setLayoutData(grdData);
		btnCancel.setText("&Cancel");
		btnCancel.addSelectionListener(new CancelSelectionListener(parent.getShell()));
		txtCode.setFocus();
		parent.getShell().setDefaultButton(btnCancel);
		
		return parent;
	}

}
